package org.yuxiong.utils;

import org.apache.log4j.Logger;


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtils {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ImageUtils.class);
		
	/**
	 * 传统算法的缩略图
	 * @param sourceFile
	 * @param tagpath
	 * @param tagsize
	 */
	public static void resizeImage(String sourceFile, String tagpath, int tagsize) {
		BufferedOutputStream bos = null;
		try {
			Image image= javax.imageio.ImageIO.read(new File(sourceFile));
            if(image!=null){
				int width=image.getWidth(null);                                     //�õ�Դͼ��
	            int height=image.getHeight(null);   
	            int new_w=0,new_h=0;
	            
	            float tempdouble;
	            if(width > tagsize && height > tagsize){
		            if(width>height){
		                tempdouble=width/tagsize;
		            }else{
		                tempdouble=height/tagsize;
		            }
		            new_w=Math.round(width/tempdouble);
		            new_h=Math.round(height/tempdouble);//
	            }else if(width > tagsize){
	            	new_w=Math.round(tagsize);
	            	new_h=Math.round(height);
	            }else if(height > tagsize){
	            	new_w=Math.round(width);
	            	new_h=Math.round(tagsize);
	            }else{
	            	new_w=width;
	            	new_h=height;
	            }
	            
	            bos = new BufferedOutputStream(new FileOutputStream(tagpath));
	            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
	            BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
	            tag.getGraphics().drawImage(image,0,0,new_w,new_h,null);       //������С���ͼ
	            encoder.encode(tag);
	            bos.close();
            }
       } catch(FileNotFoundException fnfe) {
           System.out.println(fnfe);
           if(null!=bos){
				try {bos.close();} catch (IOException e) {e.printStackTrace();}
           }	
       } catch(IOException ioe) {
           System.out.println(ioe);
           if(null!=bos){
				try {bos.close();} catch (IOException e) {e.printStackTrace();}
           }
       }
	}
	
	/**
	 * 使用ImageScale类生成的高清晰度缩略图
	 * @param sourceFile
	 * @param tagpath
	 * @param tagsize
	 */
	public static void resizeImageScale(String sourceFile, String distpath, int tagsize) {
		
		resizeImageScale(new File(sourceFile), new File(distpath), tagsize);
	}
	
	/**
	 * 使用ImageScale类生成的高清晰度缩略图
	 * @param sourceFile
	 * @param tagpath
	 * @param tagsize
	 */
	public static boolean resizeImageScale(File sourceFile, File distFile, int tagsize) {
		
		boolean isResized = true;
		BufferedOutputStream bos = null;
		try {
			BufferedImage srcBufferImage= javax.imageio.ImageIO.read(sourceFile);
			if(null != srcBufferImage){
	            int width=srcBufferImage.getWidth(null);                                     //�õ�Դͼ��
	            int height=srcBufferImage.getHeight(null);   
	            int new_w=0,new_h=0;
	            
	            float tempdouble;
	            if(width > tagsize && height > tagsize){
		            if(width>height){
		                tempdouble=width/tagsize;
		            }else{
		                tempdouble=height/tagsize;
		            }
		            new_w=Math.round(width/tempdouble);
		            new_h=Math.round(height/tempdouble);//
	            }else if(width > tagsize){
	            	new_w=Math.round(tagsize);
	            	new_h=Math.round(height);
	            }else if(height > tagsize){
	            	new_w=Math.round(width);
	            	new_h=Math.round(tagsize);
	            }else{
	            	new_w=width;
	            	new_h=height;
	            	isResized = false;
	            }
	            
	            if(isResized){
	            	logger.info("//--------------------------------> 开始生成图片缩略图... ");
		            ImageScale is = new ImageScale();
		 		   	BufferedImage tag = is.imageZoomOut(srcBufferImage,new_w,new_h);
		 		   
		 		   	bos = new BufferedOutputStream(new FileOutputStream(distFile));
		            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);    //������С���ͼ
		            encoder.encode(tag);
		            bos.close();
		            logger.info("//--------------------------------> 缩略图生成成功！生成文件： "+distFile.getPath());
	            }
			}
       } catch(FileNotFoundException fnfe) {
            System.out.println(fnfe);
            if(null!=bos){
				try {bos.close();} catch (IOException e) {e.printStackTrace();}
            }	
       } catch(IOException ioe) {
            System.out.println(ioe);
            if(null!=bos){
				try {bos.close();} catch (IOException e) {e.printStackTrace();}
            }
       }
       return isResized;
	}
	
	private static File getDistFile(File sourceFile){
		String fileName = sourceFile.getName();
		int lastIndex = fileName.lastIndexOf(".");
		
		return new File(sourceFile.getParent()+File.separator+
				fileName.substring(0, lastIndex)+"_s"+fileName.substring(lastIndex));
	}
	
	public static File resizeImageScale(File sourceFile, int tagsize) {
		
		File distFile = getDistFile(sourceFile);
		if(resizeImageScale(sourceFile, distFile, tagsize))
			return distFile;
		else
			return sourceFile;
	}
	
	public static void main(String[] args) {
		//PrintUtils.print("fdsafsdafds");
		File dist = resizeImageScale(new File("d:\\mixiu\\QshowMe.com_2E21780371.jpg"), 180);
		System.out.println(dist);
		//System.out.println(2256/100);
		//download("http://61.134.37.53:8020/6qu-com/Top500/隐形的翅膀(张韶涵).wma","c:\\1759健康金矿就.wma");
	}
}
